package com.jia.train.member.controller;

import com.jia.train.global.response.BaseResponse;
import com.jia.train.member.request.MemberLoginRequest;
import com.jia.train.member.request.MemberRegisterRequest;
import com.jia.train.member.request.MemberSendCodeRequest;
import com.jia.train.member.response.MemberLoginResponse;
import com.jia.train.member.service.MemberService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberController {

    @Resource
    private MemberService memberService;

    @GetMapping("/count")
    public BaseResponse<Integer> count() {
        BaseResponse<Integer> response = new BaseResponse<>();
        response.setData(memberService.count());
        response.setCode(200);
        response.setSuccess(true);
        return response;
    }

    @PostMapping("/insert")
    public BaseResponse<String> register(@Valid MemberRegisterRequest request) {
        memberService.register(request.getMobile());
        return new BaseResponse<>("success");
    }

    @PostMapping("/send")
    public BaseResponse<String> send(@Valid @RequestBody MemberSendCodeRequest request) {
        String result = memberService.sendCode(request.getMobile());
        return new BaseResponse<>(result);
    }

    @PostMapping("/login")
    public BaseResponse<MemberLoginResponse> Login(@Valid MemberLoginRequest request) {
        MemberLoginResponse result = memberService.login(request);
        return new BaseResponse<>(result);
    }

}
